package com.example.rubbish;

import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.rubbish.DataBase.HDataBase;
import com.example.rubbish.DataBase.History;
import com.example.rubbish.Fragment.HistoryFragment;
import com.example.rubbish.Ui.ClearEditText;
import com.example.rubbish.Utils.SystemUiOperate;

/**
 * SearchActivity:
 *     {
 *         searchBar(LinearLayout.horizontal):
 *         {
 *             clear_text(ClearEditText);
 *             back_to_home(Button);
 *         };
 *         part_hot(LinearLayout.vertical):
 *         {
 *             top_margin1(View);
 *             hot_hint(ConstraintLayout):
 *             {
 *                 textview_hot(TextView);
 *             };
 *             flexBox_hot(FlexboxLayout):
 *             {
 *                 ;
 *             };
 *         };
 *         part_history(LinearLayout.vertical):
 *         {
 *             top_margin2(View);
 *             history_hint(ConstraintLayout):
 *             {
 *                 textview_history(TextView);
 *                 history_del(Button);
 *             };
 *             flexBox_history(FlexboxLayout):
 *             {
 *                 ;
 *             };
 *         }
 *     }
 *
 *
 *
 */

//private ClearEditText clearEditText;
//private String searchContent;
//private Button button_backHome;
//private FlexboxLayout Layout_hot;
//private Button button_historyDel;;
//private FlexboxLayout Layout_history;





public class SearchActivity extends AppCompatActivity  {
    private ClearEditText clearEditText;
    private String searchContent;
    private Button button_backHome;
    private HDataBase db;
    private HistoryFragment historyFragment;
//    private HotFragment hotFragment;


    @Override
    protected void onPause() {
        super.onPause();
        historyFragment.loadHistoryView();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SystemUiOperate.hide(getSupportActionBar(), getWindow());
        setContentView(R.layout.activity_search);
        db = HDataBase.getInstance(this);
        clearEditText=findViewById(R.id.clear_text) ;
        button_backHome=findViewById(R.id.back_to_home);
        historyFragment = (HistoryFragment) getSupportFragmentManager().findFragmentById(R.id.part_history);



        clearEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
                if(actionId== EditorInfo.IME_ACTION_SEARCH){
                    if(textView.length()==0){
                        Toast toast = Toast.makeText(SearchActivity.this, "搜索内容不能为空", Toast.LENGTH_LONG);
                        toast.setGravity(Gravity.CENTER,0,0);
                        toast.show();
                    }else {
                        searchContent=textView.getText().toString();
                        //保存数据
                        saveHistory(searchContent);

                        historyFragment.loadHistoryView();
                        textView.setText("");
                        jumpToResult(searchContent);


                    }
                }
                //true显示软键盘。false隐藏软键盘
                return false;
            }
        });

        button_backHome.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

    }//onCreate()

    private void jumpToResult(String searchContent) {
        Intent intent = new Intent(SearchActivity.this,ResultActivity.class);
        intent.putExtra("searchContent", searchContent);
        intent.putExtra("searchType",0);
        startActivity(intent);
    }

    private void saveHistory(String searchContent) {
        //打开DataBase  "History"
        History history =new History(searchContent);
        db.saveHistory(history);
    }


}